#include <stdlib.h>
#include <stdio.h>

/* god bless euclid */
int gcd(int a, int b) {
    int t;
    while (b) {
        t = a;
        a = b;
        b = t % b;
        //printf("t %i a %i b %i\n", t, a, b);
    }
    return a;
}

void simplifyfraction(int *num, int *denom) {
    int n = *num;
    int d = *denom;

    /* check for div by 0 */
    if (d == 0) {
        fprintf(stderr, "division by 0, result undefined\n");
        exit(1);
    }

    /* get gcd */
    int comdiv = gcd(n, d);

    /* reduce */
    n = n / comdiv;
    d = d / comdiv;

/*
    if (n == 1) {
        printf("simplified to %i\n", n);
    } else if(comdiv == 1) {
        printf("already at reduced state %i/%i\n", n, d);
    } else {
        printf("simplified to %i/%i\n", n, d);
    }
*/

    *num = n;
    *denom = d;
}

int main(int argc, char **argv) {
    if (argc < 3) {
        fprintf(stderr, "usage: arcalc width height\n");
        return 1;
    }

    int w, h;
    sscanf(argv[1], "%i", &w);
    sscanf(argv[2], "%i", &h);

    printf("width %i height %i\n", w, h);

    simplifyfraction(&w, &h);

    printf("aspect ratio is %ix%i\n", w, h);

    return 0;
}
